home *** CD-ROM | disk | FTP | other *** search
/ Best of www.BestZips.com (Collector's Edition) / Best of WWW.BESTZIPS.COM Collector's Edition (JCSM Shareware) (JCS Marketing).ISO / prgtools / tn2.zip / COMPLEX.T < prev    next >
Text File  |  1996-11-15  |  3KB  |  147 lines

  1. %
  2. % complex.t contains complex arithmetic functions
  3. % for use in programs; to use add this file name
  4. % to the list of project files in your "*.prt" file
  5. %
  6. %   Sample program for the T Interpreter by:
  7. %
  8. %   Stephen R. Schmitt
  9. %   962 Depot Road
  10. %   Boxborough, MA 01719
  11. %
  12.  
  13. type complex : record
  14.  
  15.                re : real
  16.                im : real
  17.  
  18.                end record
  19.  
  20. %
  21. % complex assignment
  22. %
  23. procedure cmov( var dest, srce : complex )
  24.  
  25.     dest.re := srce.re
  26.     dest.im := srce.im
  27.  
  28. end procedure   
  29.  
  30. %
  31. % complex addition
  32. %
  33. procedure cadd( var dest, srce : complex )
  34.  
  35.     dest.re := dest.re + srce.re
  36.     dest.im := dest.im + srce.im
  37.  
  38. end procedure
  39.  
  40. %
  41. % complex subtraction
  42. %
  43. procedure csub( var dest, srce : complex )
  44.  
  45.     dest.re := dest.re - srce.re
  46.     dest.im := dest.im - srce.im
  47.  
  48. end procedure
  49.  
  50. %
  51. % complex multiplication
  52. %
  53. procedure cmul( var dest, srce : complex )
  54.  
  55.     var temp : complex
  56.     
  57.     temp.re := dest.re * srce.re - dest.im * srce.im
  58.     temp.im := dest.im * srce.re + dest.re * srce.im
  59.     dest.re := temp.re
  60.     dest.im := temp.im
  61.  
  62. end procedure
  63.  
  64. %
  65. % complex division
  66. %
  67. procedure cdiv( var dest, srce : complex )
  68.  
  69.     var d : real
  70.     var temp : complex
  71.  
  72.     d := srce.re * srce.re + srce.im * srce.im
  73.     temp.re := ( dest.re * srce.re + srce.im * dest.im ) / d
  74.     temp.im := ( dest.im * srce.re - dest.re * srce.im ) / d
  75.     dest.re := temp.re
  76.     dest.im := temp.im
  77.  
  78. end procedure
  79.  
  80. %
  81. % complex absolute magnitude
  82. %
  83. function cabs( var a : complex ) : real
  84.  
  85.     var rv : real
  86.  
  87.     rv := sqrt( a.re * a.re + a.im * a.im )
  88.  
  89.     return rv
  90.  
  91. end function
  92.  
  93. %
  94. % convert cartesian coordinates to angle
  95. %
  96. function argmt( y, x : real ) : real
  97.  
  98.     var rv : real
  99.     
  100.     rv := arctanxy( x, y )
  101.  
  102.     if rv < 0.0 then
  103.  
  104.         rv := rv + 2 * arcsin( 1 )
  105.  
  106.     end if
  107.  
  108.     return rv
  109.  
  110. end function
  111.  
  112. %
  113. % complex square root
  114. %
  115. procedure csqrt( var z, ans : complex )
  116.  
  117.     var x, y, r, a : real
  118.  
  119.     r := sqrt( sqrt( z.re * z.re + z.im * z.im ) )
  120.     a := 0.5 * argmt( z.im, z.re )
  121.  
  122.     ans.re := r * cos( a )
  123.     ans.im := r * sin( a )               
  124.  
  125. end procedure
  126.  
  127. %
  128. % convert complex number to a string
  129. %
  130. function cstr( var c : complex ) : string
  131.  
  132.     var s : string
  133.     var re, im : real
  134.  
  135.     re := c.re
  136.     im := c.im
  137.  
  138.     if im >= 0.0 then
  139.         s := frealstr(re,14,9 ) & " +" & frealstr(im,12,9 ) & "j"
  140.     else
  141.         im := -im
  142.         s := frealstr(re,14,9 ) & " -" & frealstr(im,12,9 ) & "j"
  143.     end if
  144.  
  145.     return s
  146.  
  147. end function